﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NP.Model;

namespace NP.BL
{
    public class TotalProductsSold
    {
        public TotalProductSoldViewModel[] TotalProducts()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                var products = db.Products.ToArray()
                    .Select(n => new TotalProductSoldViewModel
                    {
                        ProductID = n.ProductID,
                        Product = n.ProductName,
                        TotalSold = n.Order_Details.Sum(m => m.Quantity)
                    }).OrderByDescending(m => m.TotalSold).ToArray();

                return products;
            }
        }
    }
}
